<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/25
 * Time: 14:46
 */

namespace app\api\home;


class Goods extends Common
{
    // 统计商品数量
    public function count()
    {
        $goodsCount = db('goods')->where('is_sell',1)->count();
        $data = [
            'goodsCount' => $goodsCount
        ];
        return _json(0, '执行成功',$data);
    }

    // 商品列表
    public function lists()
    {
        $categoryId = input('get.categoryId',0);
        $page = input('get.page',1);
        $size = input('get.size',10);
        // 如果$categoryId是一级分类，默认遍历出所有二级分类
        $category_child = self::category_child($categoryId);
        $goods_list = db('goods')->whereIn('category_id',$category_child)->order('sort_order desc')->where('is_sell',1)->page($page,$size)->select();
        foreach ($goods_list as $k2=>$v2){
            $goods_list[$k2]['thumb_url'] = 'http://'.$host = $_SERVER['HTTP_HOST'].get_file_path($v2['thumb_url']);
        }
        $goods_count = db('goods')->whereIn('category_id',$category_child)->where('is_sell',1)->count();
        $data = [
            'goodsList' => $goods_list,
            'goodsCount' => $goods_count
        ];
        return _json(0, '执行成功',$data);
    }

    // 获取一级分类下的二级分类
    private function category_child($id=0){
        $items = [intval($id)];
        $list = db('category')->where('parent_id', $id)->column('id');

        $items = array_merge($items,$list);
        return$items;
    }


    // 商品详情
    public function detail()
    {
        $id = input('get.id',0);
        $user_id = input('get.userId',1);
        $info = db('goods')->where('id',$id)->find();
        $photos_url = explode(",",$info['photos_url']);
        foreach ($photos_url as $k=>$v){
            $photos_url[$k] = 'http://'.$host = $_SERVER['HTTP_HOST'].get_file_path($v);
        }
        $info['thumb_url'] = 'http://'.$host = $_SERVER['HTTP_HOST'].get_file_path($info['thumb_url']);
        $info['photos_url'] = $photos_url;

        $type_id =  $info['type_id'];
        $attr_list = [];
        $spec_list = [];
        $spec_info = [];
        // 商品有设置模型
        if($type_id != 0){
            // 属性
            $attr_list = db('goods_attr')->alias('ga')->join('attr a','a.attr_id=ga.attr_id')
                ->field('ga.attr_id,ga.attr_value,goods_attr_id,a.attr_name')
                ->where('ga.goods_id',$id)->order('a.sort_order desc')->select();
            // 规格
            $spec_list = db('spec')->where("type_id",$type_id)->order('sort_order desc')->select();
            foreach($spec_list as $k => $v){
                $spec_list[$k]['spec_item'] = db('spec_item')->where("spec_id",$v['id'])->order('id')->column('item,checked','id'); // 获取规格项

            }

            $items_id = db('spec_price')->where('goods_id',$id)->column( "key");
            $spec_info = db('spec_price')->where('goods_id',$id)->column('key,key_name,price,store_num,sku','key');//规格信息
            $items_ids = [];
            // 将 8_11 拆分为 8,11
            foreach ($items_id as $k=>$v){
                $_item = explode('_', $v);
                $items_ids = array_merge($items_ids,$_item);
            }
            // 将该商品未设置的属性去除
            foreach ($spec_list as $k=>$v){
                foreach ($v['spec_item'] as $k2=>$v2){
                    if(!in_array($k2,$items_ids)){
                        unset($spec_list[$k]['spec_item'][$k2]);
                    }
                }
            }
        }


        // 是否收藏了
        $has = db('collect')->where([
            'user_id'=>$user_id,
            'type_id'=>0,
            'value_id'=>$id
        ])->find();
        // 查询商品是无模型规格0、还是有规格模型1
        $goods_type = 1;
        if(empty($spec_list)||empty($spec_info)){
            $goods_type = 0;
        }
        if($info['type_id'] == 0){
            $goods_type = 0;
        }
        $data = [
            'info' => $info,
            'goods_type' => $goods_type,
            'attrList' => $attr_list,
            'specList' => $spec_list,
            'specInfo' => $spec_info,
            'userHasCollect' => $has?true:false
        ];
        return _json(0, '执行成功',$data);
    }

    // 分类页面数据
    public function category()
    {
        $id = input('get.id',0);    //分类
        $category_info = db('category')->where('id',$id)->find();
        if($category_info['parent_id'] == 0){
            $parent_id = $category_info['id'];
            $brother_list = db('category')->where('parent_id',$parent_id)->order('sort_order desc')->select();
            $current_info = $category_info;
        }else{
            $current_info = db('category')->where('id',$id)->find();
            // 兄弟
            $brother_list = db('category')->where('parent_id',$current_info['parent_id'])->order('sort_order desc')->select();
        }
//        if($category_info['parent_id'] == 0){
//            $parent_id = $category_info['id'];
//            $brother_list = db('category')->where('parent_id',$parent_id)->order('sort_order desc')->select();
//            $current_info = db('category')->where('id',$brother_list[0]['id'])->find();
//        }else{
//            $current_info = db('category')->where('id',$id)->find();
//            // 兄弟
//            $brother_list = db('category')->where('parent_id',$current_info['parent_id'])->order('sort_order desc')->select();
//        }

        $data = [
            'brotherCategory' => $brother_list,   //兄弟
            'currentCategory' => $current_info   //自己
        ];

        return _json(0, '执行成功',$data);
    }
}